package com.kgc.springboot.hangduo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.mapper.FeedbackMapper;
import com.kgc.springboot.hangduo.pojo.Feedback;
import com.kgc.springboot.hangduo.pojo.FeedbackExample;
import com.kgc.springboot.hangduo.service.FeedbackService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: hangduo
 * @author: Manxj
 * @create: 2020-12-28 17:25
 */
@Service
public class FeedbackServiceImpl implements FeedbackService{
    @Resource
    FeedbackMapper feedbackMapper;

    @Override
    public PageInfo<Feedback> selectByExample(Integer type, String phone, String content, Integer pageNum) {
        PageHelper.startPage(pageNum,8);
        PageHelper.orderBy("id");
        FeedbackExample feedbackExample = new FeedbackExample();

        if(type==5&&phone=="%%"&&content=="%%"){
            feedbackExample=null;
        }else {
            if(type==5){
                feedbackExample.createCriteria().andPhoneLike(phone).andContentLike(content);
            }else{
                feedbackExample.createCriteria().andPhoneLike(phone).andTypeEqualTo(type).andContentLike(content);
            }
        }
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<Feedback> pageInfo = new PageInfo<Feedback>(feedbacks);
        return pageInfo;
    }

    @Override
    public Feedback selectById(Integer id) {
        FeedbackExample feedbackExample = new FeedbackExample();
        feedbackExample.createCriteria().andIdEqualTo(id);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        return feedbacks.size()>0?feedbacks.get(0):null;
    }

    @Override
    public Integer updateById(Feedback feedback) {
        int i = feedbackMapper.updateByPrimaryKeySelective(feedback);
        return i;
    }

    @Override
    public List<Feedback> selectOne() {
        List<Feedback> feedbacks = feedbackMapper.selectByExample(null);
        return feedbacks;
    }

    @Override
    public List<Feedback> selectMy(Integer id) {
        FeedbackExample feedbackExample = new FeedbackExample();
        feedbackExample.createCriteria().andUidEqualTo(id);
        List<Feedback> list = feedbackMapper.selectByExample(feedbackExample);
        return list;
    }

    @Override
    public Integer insert(Feedback feedback) {
        int i = feedbackMapper.insertSelective(feedback);
        return i;
    }
}
